﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SMSManage
{
    public partial class frmPopUpStudentParent : Form
    {
        #region VariablesPropertiesConstructors
        /// <summary>
        /// Declare member variables here
        /// </summary>
        int _ParentID = 0;

        public frmPopUpStudentParent()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Properties here
        /// </summary>
        public int ParentID 
        {
            get 
            { 
                return _ParentID; 
            }
            set 
            { 
                _ParentID = value; 
            }
        }
        #endregion VariablesPropertiesConstructors

        #region LoadData
        /// <summary>
        /// Form loaded
        /// </summary>
        private void frmPopUpStudentParent_Load(object sender, EventArgs e)
        {
            if (this._ParentID != 0)
            {
                DatasetXSD.ParentTableAdapters.tbl_ParentTableAdapter dp = new 
                    DatasetXSD.ParentTableAdapters.tbl_ParentTableAdapter();
                this.Text += 
                    " " + dp.Get_tbl_Parent_Select_All_By_ParentID(this._ParentID).Rows[0]["ParentName"].ToString();
                BindByStudent();

            }
        }

        /// <summary>
        /// Lấy học viên theo phụ huynh
        /// </summary>
        private void BindByStudent()
        {
            DatasetXSD.StudentTableAdapters.tbl_Student_Select_AllTableAdapter dp =
                new DatasetXSD.StudentTableAdapters.tbl_Student_Select_AllTableAdapter();
            DataTable dttbl = dp.Get_tbl_Student_Select_All_By_ParentID(this._ParentID);
           
            //Add to the listview
            for (int i = 0; i < dttbl.Rows.Count; i++)
            {
                ListViewItem item = new ListViewItem(dttbl.Rows[i]["StudentName"].ToString());
                item.SubItems.Add(dttbl.Rows[i]["StudentAddress"].ToString());
                item.SubItems.Add(dttbl.Rows[i]["StudentID"].ToString());
                this.listViewSelected.Items.Add(item);
            }

            //Bind to the 'all' listbox
            BindAll();
        }

        /// <summary>
        /// Lấy tất cả học viên trừ những học viên xuất hiện bên listView kia
        /// </summary>
        private void BindAll()
        {
            DatasetXSD.StudentTableAdapters.tbl_Student_Select_AllTableAdapter dp
                = new DatasetXSD.StudentTableAdapters.tbl_Student_Select_AllTableAdapter();
            DataTable dttbl = dp.Get_tbl_Student_Select_All();
            for (int i = 0; i < dttbl.Rows.Count; i++)
            {
                ListViewItem item = new ListViewItem(dttbl.Rows[i]["StudentName"].ToString());
                item.SubItems.Add(dttbl.Rows[i]["StudentAddress"].ToString());
                item.SubItems.Add(dttbl.Rows[i]["StudentID"].ToString());
                bool ok = false;
                for (int j = 0; j < this.listViewSelected.Items.Count; j++)
                {
                    if (this.listViewSelected.Items[j].SubItems[2].Text.ToString() ==
                        item.SubItems[2].Text.ToString())
                    {
                        ok = true;
                        break;
                    }
                }
                if (!ok)
                {
                    this.listViewAll.Items.Add(item);
                }
            }
        }
        #endregion LoadData

        #region Search

        /// <summary>
        /// Tim kiem
        /// </summary>
        private void btnOK_Click(object sender, EventArgs e)
        {
            SearchStudents();
        }

        /// <summary>
        /// Tim kiem theo ten
        /// </summary>
        private void SearchStudents()
        {
            //xoa tat ca
            this.listViewAll.Items.Clear();
            
            DatasetXSD.StudentTableAdapters.tbl_Student_Select_AllTableAdapter dp
                = new DatasetXSD.StudentTableAdapters.tbl_Student_Select_AllTableAdapter();

            DataTable dttbl = 
                dp.Get_tbl_Student_Search_All_By_StudentName(this.txt_Name.Text.Trim());
            
            for (int i = 0; i < dttbl.Rows.Count; i++)
            {
                ListViewItem item = new ListViewItem(dttbl.Rows[i]["StudentName"].ToString());
                item.SubItems.Add(dttbl.Rows[i]["StudentAddress"].ToString());
                item.SubItems.Add(dttbl.Rows[i]["StudentID"].ToString());
                bool ok = false;
                for (int j = 0; j < this.listViewSelected.Items.Count; j++)
                {
                    if (this.listViewSelected.Items[j].SubItems[2].Text.ToString() ==
                        item.SubItems[2].Text.ToString())
                    {
                        ok = true;
                        break;
                    }
                }
                if (!ok)
                {
                    this.listViewAll.Items.Add(item);
                }
            }
        }
        #endregion Search

        #region AddRemoveList

        /// <summary>
        /// Add to the 'selected' listbox
        /// </summary>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                //Add to the selected listview
                if (this.listViewAll.SelectedItems.Count > 0) //At least 1 item selected
                {
                    ListViewItem selectedItem = this.listViewAll.SelectedItems[0];
                    this.listViewAll.Items.Remove(selectedItem);
                    this.listViewSelected.Items.Add(selectedItem);
                }
            }
            catch (Exception exc)
            {
                exc.ToString();
            }

        }

        /// <summary>
        /// Add to the selected listview
        /// </summary>
        private void btn_RemoveSelected_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.listViewSelected.SelectedItems.Count > 0) //At least 1 item selected
                {
                    ListViewItem selectedItem = this.listViewSelected.SelectedItems[0];
                    this.listViewSelected.Items.Remove(selectedItem);
                    this.listViewAll.Items.Add(selectedItem);
                }
            }
            catch (Exception exc)
            {
                exc.ToString();
            }
        }

        /// <summary>
        /// Remove all selected items
        /// </summary>
        private void btn_RemoveAllSelected_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.listViewSelected.Items.Count > 0)
                {
                    for (int i = 0; i < this.listViewSelected.Items.Count; i++)
                    {
                        ListViewItem selectedItem = this.listViewSelected.Items[i];
                        this.listViewSelected.Items.Remove(selectedItem);
                        this.listViewAll.Items.Add(selectedItem);
                        i = -1;
                    }
                }
            }
            catch (Exception exc)
            {
                exc.ToString();
            }
        }

        #endregion AddRemoveList

        #region DisplayInfo
        /// <summary>
        /// listViewAll selected index changed
        /// </summary>
        private void listViewAll_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listViewAll.SelectedItems.Count > 0)
            {
                this.lblDetail.Text = this.listViewAll.SelectedItems[0].SubItems[0].Text.ToString() +
                               "-" + this.listViewAll.SelectedItems[0].SubItems[1].Text.ToString();
            }
        }

        #endregion DisplayInfo

        #region Save

        /// <summary>
        /// Save data
        /// </summary>
        private void btn_Save_Click(object sender, EventArgs e)
        {
            //Delete all
            try
            {
                DatasetXSD.StudentParentTableAdapters.tbl_Student_Parent_Select_AllTableAdapter dp
                    = new DatasetXSD.StudentParentTableAdapters.tbl_Student_Parent_Select_AllTableAdapter();
                dp.tbl_Student_Parent_Delete_By_ParentID(this._ParentID);
                int count = 0;
                for (int i = 0; i < this.listViewSelected.Items.Count; i++)
                {
                    int studentID = Convert.ToInt32(this.listViewSelected.Items[i].SubItems[2].Text.ToString());
                    int ok = dp.tbl_Student_Parent_Insert_All(studentID, this._ParentID);
                    if (ok > 0)
                    {
                        count++;
                    }
                }
                if (count > 0 || this.listViewSelected.Items.Count == 0)
                {
                    MessageBox.Show("Lưu thành công", "Thành công", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception exc)
            {
                exc.ToString();
            }
        }

        #endregion Save

        #region Exit
        /// <summary>
        /// Close the form
        /// </summary>
        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
        #endregion Exit
    }
}
